/**
 * Created by dengyuqing on 2017/8/3.
 */
var gulp = require('gulp');
var concat = require('gulp-concat'); //- 多个文件合并为一个；
var uglify = require('gulp-uglify');
var clean = require('gulp-clean');
var webserver = require('gulp-webserver');
var webConnect = require('gulp-connect');
var html_min = require('gulp-htmlmin');
var image_min = require('gulp-imagemin');
var minifyCss = require('gulp-minify-css');
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var runSequence = require('gulp-sequence');

gulp.task('cleanwxPc', function() {
	return gulp.src(['wxPc'])
		.pipe(clean())
});
//清理掉此js

gulp.task('copy', function() {
	gulp.src(['wxPcAll/.idea/**'])

	.pipe(gulp.dest('wxPc/.idea'));

	gulp.src(['wxPcAll/css/*.css'])
		.pipe(minifyCss())
		.pipe(gulp.dest('wxPc/css'));

	gulp.src(['wxPcAll/images/**'])

	.pipe(gulp.dest('wxPc/images'));

	gulp.src(['wxPcAll/js/*.js'])
		.pipe(uglify())
		.pipe(gulp.dest('wxPc/js'));

	gulp.src(['wxPcAll/public/**'])

	.pipe(gulp.dest('wxPc/public'));
	
	gulp.src(['wxPcAll/fonts/**'])

	.pipe(gulp.dest('wxPc/fonts'));
	
	gulp.src(['wxPcAll/video/**'])

	.pipe(gulp.dest('wxPc/video'));
	
	gulp.src(['wxPcAll/wx/**'])

	.pipe(gulp.dest('wxPc/wx'));
	gulp.src(['wxPcAll/Chinese/**'])

	.pipe(gulp.dest('wxPc/Chinese'));
	

	gulp.src(['wxPcAll/wxPcSystem/bootstrap-3.3.7-dist/**'])

	.pipe(gulp.dest('wxPc/wxPcSystem/bootstrap-3.3.7-dist'));

	gulp.src(['wxPcAll/wxPcSystem/bootstrap-table-master/**'])

	.pipe(gulp.dest('wxPc/wxPcSystem/bootstrap-table-master'));
	
	gulp.src(['wxPcAll/wxPcSystem/layui-v2.2.4/**'])

	.pipe(gulp.dest('wxPc/wxPcSystem/layui-v2.2.4'));

	gulp.src(['wxPcAll/wxPcSystem/css/*.css'])
		.pipe(minifyCss())
		.pipe(gulp.dest('wxPc/wxPcSystem/css'));
		
    gulp.src(['wxPcAll/wxPcSystem/img/**'])
		
		.pipe(gulp.dest('wxPc/wxPcSystem/img'));
		
	gulp.src(['wxPcAll/wxPcSystem/js/*.js'])
		.pipe(uglify())
		.pipe(gulp.dest('wxPc/wxPcSystem/js'));

	

});

gulp.task('connect', function() {
	webConnect.server({
		root: 'wxPc',
		port: 8000,
		livereload: true
	});
})

gulp.task('browserSync', function() {
	browserSync.init({
		proxy: '192.168.1.25',
		port: 8000
	});
});

gulp.task('indexHtml', function() {
	var options = {
		removeComments: true, //清除HTML注释
		collapseWhitespace: true, //压缩HTML
		collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
		removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
		removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
		removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
		minifyJS: true, //压缩页面JS
	};
	gulp.src('wxPcAll/index.html')
		.pipe(html_min(options))
		.pipe(gulp.dest('wxPc'))
	gulp.src('wxPcAll/*.html')
		.pipe(html_min(options))
		.pipe(gulp.dest('wxPc'))
	gulp.src('wxPcAll/common/*.html')
		.pipe(html_min(options))
		.pipe(gulp.dest('wxPc/common'))
	gulp.src('wxPcAll/wxPcSystem/*.html')
		.pipe(html_min(options))
		.pipe(gulp.dest('wxPc/wxPcSystem'))
	gulp.src('wxPcAll/wxPcSystem/html/**')
		.pipe(html_min(options))
		.pipe(gulp.dest('wxPc/wxPcSystem/html'))
})

gulp.task('start_pro', function(cb) {
	runSequence('indexHtml', 'copy', 'connect', 'browserSync')(cb)
})

gulp.task('default', ['cleanwxPc'], function() {
	gulp.start(['start_pro']);
	gulp.watch(['wxPcAll/index.html', 'wxPcAll/html/*.html'])
	gulp.watch(['wxPcAll/config.js', 'wxPcAll/directive/*.js', 'wxPcAll/controller/*.js'])
	gulp.watch(['wxPcAll/css/*.css', 'wxPcAll/fonts/**']);
	console.log([
		"                   _ooOoo_",
		"                  o8888888o",
		"      佛          88\" . \"88            P",
		"      祖          (| -_- |)             I",
		"      保          O\\  =  /O             B",
		"      佑       ____/`---'\\____         8",
		"             .'  \\\\|     |//  `.",
		"            /  \\\\|||  :  |||//  \\",
		"           /  _||||| -:- |||||-  \\",
		"           |   | \\\\\\  -  /// |   |",
		"           | \\_|  ''\\---/''  |   |",
		"           \\  .-\\__  `-`  ___/-. /",
		"         ___`. .'  /--.--\\  `. . __",
		"      .\"\" '<  `.___\\_<|>_/___.'  >'\"\".",
		"     | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |",
		"     \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /",
		"======`-.____`-.___\\_____/___.-`____.-'======",
		"                   `=---='",
		"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^",
		"         佛祖保佑       永无BUG"
	].join('\n'));
})